package cn.v2tila.rsosa.student.controller;

import cn.v2tila.rsosa.student.entity.*;
import cn.v2tila.rsosa.student.service.*;
import cn.v2tila.rsosa.student.vo.*;
import cn.v2tila.rsosa.student.utils.utils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author v2tila on 14:45 2019/3/15
 */
@RestController
@RequestMapping(value = "zpxx")
public class zpxxController {
    @Autowired
    private SchoolService schoolService;
    @Autowired
    private ZpxxService zpxxService;
    @Autowired
    private AssoService assoService;
    @Autowired
    private ZpxxDepService zpxxDepService;
    @Autowired
    private ZpxxImgsService zpxxImgsService;
    @Autowired
    private FollowService followService;
    @Autowired
    private DeliveryService deliveryService;
    @Autowired
    private VitaeService vitaeService;

    @RequestMapping(value = "save")
    public ResultVO save(ZpxxVO zpxxVO){
        ResultVO resultVO = new ResultVO();
        Gson gson = new Gson();
        System.out.println(zpxxVO);
        List<depList> depLists = gson.fromJson(zpxxVO.getDepList(), new TypeToken<List<depList>>() {}.getType());
        List<String> depImgs = gson.fromJson(zpxxVO.getDepImgs(), new TypeToken<List<String>>() {}.getType());
        String assoName = zpxxVO.getAssoName();
        String assoType = zpxxVO.getAssoType();
        String avatarUrl = zpxxVO.getAvatarUrl();
        String schoolName = zpxxVO.getSchoolName();
        String collegeName = zpxxVO.getCollegeName();
        String studentName = zpxxVO.getStudentName();
        String stjj = zpxxVO.getStjj();
        String stxq = zpxxVO.getStxq();
        Integer assoId = assoService.findBySchoolNoAndName(schoolService.findByName(schoolName).getNo(), assoName).getId();
        ZpxxEntity zpxxEntity = new ZpxxEntity();
        zpxxEntity.setAssoId(assoId);
        zpxxEntity.setAssoName(assoName);
        zpxxEntity.setAssoType(assoType);
        zpxxEntity.setAvatarUrl(avatarUrl);
        zpxxEntity.setCollegeName(collegeName);
        zpxxEntity.setSchoolName(schoolName);
        zpxxEntity.setStudentName(studentName);
        zpxxEntity.setStjj(stjj);
        zpxxEntity.setStxq(stxq);
        zpxxEntity.setCreateTime(new utils().getTime());
        zpxxEntity.setStartTime(zpxxEntity.getCreateTime().substring(5, 10));
        if(depImgs.size()!=0){
            zpxxEntity.setHasImgs(1);
        }
        if(depLists.size()!=0){
            zpxxEntity.setHasDep(1);
        }
        System.out.println(zpxxEntity);
        ZpxxEntity zpxxIsSuc = zpxxService.saveAndFlush(zpxxEntity);
        if (zpxxIsSuc == null) {
            resultVO.setCode(0);
            resultVO.setMsg("招聘信息存储失败");
        }else{
            if(zpxxIsSuc.getHasDep()==1){
                for(int i=0;i<depLists.size();i++){
                    ZpxxDepEntity zpxxDepEntity = new ZpxxDepEntity();
                    zpxxDepEntity.setZpxxId(zpxxIsSuc.getId());
                    zpxxDepEntity.setDepName(depLists.get(i).getDepName());
                    zpxxDepEntity.setDepDesc(depLists.get(i).getDepName());
                    ZpxxDepEntity zpxxDepIsSuc = zpxxDepService.saveAndFlush(zpxxDepEntity);
                    if(zpxxDepIsSuc == null){
                        resultVO.setCode(0);
                        resultVO.setMsg("部门信息存储失败");
                        return resultVO;
                    }
                }
            }
            if(zpxxIsSuc.getHasImgs()==1){
                for(int i=0;i<depImgs.size();i++){
                    ZpxxImgsEntity zpxxImgsEntity = new ZpxxImgsEntity();
                    zpxxImgsEntity.setZpxxId(zpxxIsSuc.getId());
                    zpxxImgsEntity.setFileId(depImgs.get(i));
                    ZpxxImgsEntity zpxxImgsIsSuc = zpxxImgsService.saveAndFlush(zpxxImgsEntity);
                    if(zpxxImgsIsSuc == null){
                        resultVO.setCode(0);
                        resultVO.setMsg("照片信息存储失败");
                        return resultVO;
                    }
                }
            }
            AssoEntity assoEntity = assoService.findById(assoId);
            assoEntity.setIsPublic(1);
            AssoEntity saveIsSuccess = assoService.saveAndFlush(assoEntity);
            if (saveIsSuccess == null) {
                resultVO.setCode(0);
                resultVO.setMsg("已发布状态修改失败");
            }else{
                resultVO.setCode(1);
                resultVO.setMsg("发布成功");
            }
        }
        return resultVO;
    }

    @RequestMapping(value = "getZpxx")
    public ResultVO getZpxx(RequestVO requestVO) {
        ResultVO resultVO = new ResultVO();
        List<ZpxxEntity> zpxxEntities = new ArrayList<ZpxxEntity>();
        if(requestVO.getName()!=null){
            String studentName = requestVO.getName();
            zpxxEntities = zpxxService.findByStudentName(studentName);
        }else if(requestVO.getUniqueId()!=null&&requestVO.getCode().equals("delivery")){
            List<DeliveryEntity> deliveryEntities = deliveryService.findByUniqueId(requestVO.getUniqueId());
            for (int i = 0; i < deliveryEntities.size(); i++) {
                ZpxxEntity zpxxEntity = zpxxService.findById(deliveryEntities.get(i).getZpxxId());
                zpxxEntities.add(zpxxEntity);
            }
        }else if(requestVO.getUniqueId()!=null){
            List<FollowEntity> followEntities = followService.findByUniqueId(requestVO.getUniqueId());
            for (int i = 0; i < followEntities.size(); i++) {
                ZpxxEntity zpxxEntity = zpxxService.findById(followEntities.get(i).getZpxxId());
                zpxxEntities.add(zpxxEntity);
            }
        }else{
            Page<ZpxxEntity> pages = zpxxService.findByCollegeName(requestVO.getCollege(), requestVO.getPage());
            zpxxEntities = pages.getContent();
            resultVO.setCount(pages.getTotalPages());
        }
        List<ZpxxVO> zpxxList = new ArrayList<ZpxxVO>();
        for(int i = 0;i<zpxxEntities.size();i++){
            ZpxxVO zpxx = new ZpxxVO();
            zpxx.setId(zpxxEntities.get(i).getId());
            zpxx.setAssoType(zpxxEntities.get(i).getAssoType());
            zpxx.setAvatarUrl(zpxxEntities.get(i).getAvatarUrl());
            zpxx.setCollegeName(zpxxEntities.get(i).getCollegeName());
            zpxx.setSchoolName(zpxxEntities.get(i).getSchoolName());
            zpxx.setAssoName(assoService.findById(zpxxEntities.get(i).getAssoId()).getName());
            zpxx.setCreateTime(zpxxEntities.get(i).getCreateTime());
            zpxx.setStartTime(zpxxEntities.get(i).getStartTime());
            zpxxList.add(zpxx);
        }
        resultVO.setZpxxList(zpxxList);

        return resultVO;
    }

    @RequestMapping(value = "searchZpxx")
    public ResultVO searchZpxx(int page,String college,String searchVal) {
        ResultVO resultVO = new ResultVO();
        List<ZpxxEntity> zpxxEntities = new ArrayList<ZpxxEntity>();
        Page<ZpxxEntity> pages = zpxxService.searchZpxx( page,college,searchVal);
        zpxxEntities = pages.getContent();
        resultVO.setCount(pages.getTotalPages());

        List<ZpxxVO> zpxxList = new ArrayList<ZpxxVO>();
        for(int i = 0;i<zpxxEntities.size();i++){
            ZpxxVO zpxx = new ZpxxVO();
            zpxx.setId(zpxxEntities.get(i).getId());
            zpxx.setAssoType(zpxxEntities.get(i).getAssoType());
            zpxx.setAvatarUrl(zpxxEntities.get(i).getAvatarUrl());
            zpxx.setCollegeName(zpxxEntities.get(i).getCollegeName());
            zpxx.setSchoolName(zpxxEntities.get(i).getSchoolName());
            zpxx.setAssoName(assoService.findById(zpxxEntities.get(i).getAssoId()).getName());
            zpxx.setCreateTime(zpxxEntities.get(i).getCreateTime());
            zpxx.setStartTime(zpxxEntities.get(i).getStartTime());
            zpxxList.add(zpxx);
        }
        resultVO.setZpxxList(zpxxList);

        return resultVO;
    }

    @RequestMapping(value = "getZpxxDetail")
    public ResultVO getZpxxDetail(Integer zpxxId){
        ResultVO resultVO = new ResultVO();
        ZpxxEntity zpxxEntity = zpxxService.findById(zpxxId);
        if(zpxxEntity!=null){
            ZpxxVO zpxx = new ZpxxVO();
            AssoEntity assoEntity = assoService.findById(zpxxEntity.getAssoId());
            zpxx.setAssoName(assoEntity.getName());
            zpxx.setStudentName(zpxxEntity.getStudentName());
            zpxx.setAssoType(zpxxEntity.getAssoType());
            zpxx.setAvatarUrl(zpxxEntity.getAvatarUrl());
            zpxx.setCollegeName(zpxxEntity.getCollegeName());
            zpxx.setSchoolName(zpxxEntity.getSchoolName());
            zpxx.setStartTime(zpxxEntity.getStartTime());
            zpxx.setCreateTime(zpxxEntity.getCreateTime());
            zpxx.setStjj(zpxxEntity.getStjj());
            zpxx.setStxq(zpxxEntity.getStxq());
            if(zpxxEntity.getHasDep()==1){
                Gson gson = new Gson();
                String depList = gson.toJson(zpxxDepService.findByZpxxId(zpxxId));
                zpxx.setDepList(depList);
            }
            if (zpxxEntity.getHasImgs() == 1) {
                Gson gson = new Gson();
                List<String> depImgs = new ArrayList<String>();
                List<ZpxxImgsEntity> depImgsEntities = zpxxImgsService.findByZpxxId(zpxxId);
                for (int i = 0; i < depImgsEntities.size(); i++) {
                    String depImg = depImgsEntities.get(i).getFileId();
                    depImgs.add(depImg);
                }
                String depImgsJson = gson.toJson(depImgs);
                zpxx.setDepImgs(depImgsJson);
            }
            resultVO.setZpxxVO(zpxx);
        }
        return resultVO;
    }
    @RequestMapping(value = "isFollow")
    public boolean isFollow(String uniqueId,Integer zpxxId){
        return followService.existsByUniqueIdAndZpxxId(uniqueId, zpxxId);
    }

    @RequestMapping(value = "isDelivery")
    public boolean isDelivery(String uniqueId, Integer zpxxId) {
        return deliveryService.existsByUniqueIdAndZpxxId(uniqueId, zpxxId);
    }
    @RequestMapping(value = "follow")
    public ResultVO followZpxx(String uniqueId,Integer zpxxId){
        ResultVO resultVO = new ResultVO();
        FollowEntity followEntity = new FollowEntity();
        followEntity.setUniqueId(uniqueId);
        followEntity.setZpxxId(zpxxId);
        FollowEntity isSuccess = followService.saveAndFlush(followEntity);
        if (isSuccess==null){
            resultVO.setCode(0);
        }else{
            resultVO.setCode(1);
        }
        return resultVO;
    }
    @RequestMapping(value = "unfollow")
    public ResultVO unfollowZpxx(String uniqueId,Integer zpxxId){
        ResultVO resultVO = new ResultVO();
        FollowEntity followEntity = followService.findByUniqueIdAndZpxxId(uniqueId, zpxxId);
        resultVO.setCode(1);
        try{
            followService.delete(followEntity);
        }catch (Exception e){
            resultVO.setCode(0);
        }
        return resultVO;
    }
    @RequestMapping(value = "delivery")
    public ResultVO delivery(String uniqueId,Integer zpxxId,String depName,String title){
        ResultVO resultVO = new ResultVO();
        VitaeEntity vitaeEntity = vitaeService.findByTitle(title);
        DeliveryEntity deliveryEntity = new DeliveryEntity();
        deliveryEntity.setUniqueId(uniqueId);
        deliveryEntity.setZpxxId(zpxxId);
        deliveryEntity.setDepName(depName);
        deliveryEntity.setVitaeId(vitaeEntity.getId());
        deliveryEntity.setCreateTime(new utils().getTime());
        ZpxxEntity zpxxEntity = zpxxService.findById(zpxxId);
        deliveryEntity.setAssoId(zpxxEntity.getAssoId());
        deliveryEntity.setCode(0);
        DeliveryEntity isSuccess = deliveryService.saveAndFlush(deliveryEntity);
        if(isSuccess==null){
            resultVO.setCode(0);
        }else resultVO.setCode(1);
        return resultVO;
    }

    @RequestMapping(value = "getVitaeTitleByOpenid")
    public String getVitaeTitleByOpenid(String uniqueId){
        List<VitaeEntity> vitaeEntityList = vitaeService.findByUniqueId(uniqueId);
        List<String> titles = new ArrayList<String>();
        for (int i = 0; i < vitaeEntityList.size(); i++) {
            String title = vitaeEntityList.get(i).getTitle();
            titles.add(title);
        }
        Gson gson = new Gson();
        String titlesJson = gson.toJson(titles);
        return titlesJson;
    }
}
